<template>
  <a-modal
    title="任务"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="内容">
          <a-textarea v-decorator="['content', {rules: [{required: true, message: '请输入内容'}]}]" />
        </a-form-item>

        <a-form-item label="图片">
          <a-upload
            v-decorator="[
              'upload',
              {
                valuePropName: 'fileList',
                getValueFromEvent: normFile,
                rules: [{required: true, message: '请上传图片'}]
              },
            ]"
            multiple
            accept="image/png, image/jpeg"
            :customRequest="customRequest"
            list-type="picture"
            @preview="handlePreview"
          >
            <a-button><a-icon type="upload" />上传</a-button>
          </a-upload>
        </a-form-item>

        <a-form-item label="接受人">
          <a-select
            v-decorator="['user_id', {rules: [{required: true, message: '请选择接受人'}]}]"
            show-search
            placeholder="请输入联系人"
            style="width: 100%"
            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            :not-found-content="null"
            @search="handleSearch"
            @change="handleChange"
          >
            <a-select-option v-for="d in userList" :key="d.id">
              {{ d.username }}
            </a-select-option>
          </a-select>
          <!-- <a-input v-decorator="['description', {rules: [{required: true, min: 5, message: '请输入至少五个字符的规则描述！'}]}]" /> -->
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { searchUser } from '@/api/user'
import { upload } from '@/api/upload'
// 表单字段
const fields = ['content', 'id', 'user_id', 'upload']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      userList: []
    }
  },
  created () {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleSearch (value) {
      console.log('handleSearch', value)
      if (value) {
        searchUser({ value }).then(res => {
          // console.log(res)
          this.userList = res.result
        }).catch(errors => {
          console.log(errors)
        })
      }
      // fetch(value, data => (this.data = data));
    },
    handleChange (value) {
      console.log('handleChange', value)
      // this.value = value;
      // fetch(value, data => (this.data = data));
    },
    normFile (e) {
      console.log('Upload event:', e)
      if (Array.isArray(e)) {
        return e
      }
      return e && e.fileList
    },
    customRequest (file) {
      // console.log('sdfddddddddddddddďdd', file)
      const params = new FormData() // 创建form对象
      params.append('file', file.file)
      // param.append('caseId', this.caseId)
      // param.append('caseClassId', this.classList[this.activIndex].id)
      // this.uploadStatus = true
      upload(params).then(res => {
        console.log(res)

        file.onSuccess(res.result)
        // this.folderClassFileList.push(res.result)
        // this.uploadStatus = false
        // this.classList[this.activIndex].case_class_file_count++
      }).catch(errors => {
        // this.uploadStatus = false
      })
    },
    async handlePreview (file) {
      console.log(file)
      const obj = file.response
      window.open(obj.host + obj.img_url)
      // if (!file.url && !file.preview) {
      //   file.preview = await getBase64(file.originFileObj);
      // }
      // this.previewImage = file.url || file.preview;
      // this.previewVisible = true;
    }
  }
}
</script>
